//
//  ByCellMemberList.m
//  mijifen_ios
//
//  Created by mc on 2017/11/13.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellMemberList.h"

// cell
#import "ByCellMemberListInfo.h"

@interface ByCellMemberList()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;

@property (nonatomic, strong) NSMutableArray *selectedAry;
@property (nonatomic, strong) NSMutableArray *cellArray;
@property (nonatomic, strong) UIButton *selectButton;
@end
@implementation ByCellMemberList
+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByCellMemberList";
    ByCellMemberList *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    CGFloat width = [@"使用条件：" getSizeOfString:16].width;
    _headerView = [[UIView alloc] init];
    [self addSubview:_headerView];
    _headerView.backgroundColor = [UIColor whiteColor];
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(15 + width));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 15 * 2 - width, 50));
    }];
    
    _selectButton = [[UIButton alloc] init];
    [_headerView addSubview:_selectButton];
    [_selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-10));
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    [_selectButton setImage:[UIImage imageNamed:@"Pay_unselected"] forState:UIControlStateNormal];
    [_selectButton setImage:[UIImage imageNamed:@"Pay_selected"] forState:UIControlStateSelected];
    [_selectButton addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];

    UILabel *allLabel = [[UILabel alloc] init];
    [_headerView addSubview:allLabel];
    width = [@"全选" getSizeOfString:16].width + 10;
    [allLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_selectButton.mas_left);
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    allLabel.text = @"全选";
    allLabel.font = FONT(16);
    allLabel.textColor = [UIColor lightGrayColor];
    
    UILabel *memberLabel = [[UILabel alloc] init];
    [_headerView addSubview:memberLabel];
    [memberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.right.equalTo(allLabel.mas_left);
        make.top.equalTo(@(10));
        make.bottom.equalTo(@(-10));
    }];
    memberLabel.textAlignment = NSTextAlignmentCenter;
    memberLabel.font = FONT(16);
    memberLabel.textColor = [UIColor lightGrayColor];
    memberLabel.text = @"会员列表";
    
    self.tableView = [[UITableView alloc] init];
    [self addSubview:self.tableView];
    self.tableView.scrollEnabled = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}

- (void)setItems:(NSMutableArray *)items{
    _items = items;
    if (self.selectedAry.count != 0) {
        [self.selectedAry removeAllObjects];
    }
    if (self.cellArray.count != 0) {
        [self.cellArray removeAllObjects];
    }
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(15));
        make.top.equalTo(_headerView.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 15 * 2, (items.count + 1) * 50));
    }];
    [self.tableView reloadData];
}

#pragma mark - getter
- (NSMutableArray *)selectedAry{
    if (!_selectedAry) {
        _selectedAry = [NSMutableArray array];
    }
    return _selectedAry;
}

- (NSMutableArray *)cellArray{
    if (!_cellArray) {
        _cellArray = [NSMutableArray array];
    }
    return _cellArray;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellMemberListInfo *cell = [ByCellMemberListInfo cellWithTableView:tableView];
    cell.model = self.items[indexPath.row];
    [self.cellArray addObject:cell];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 15 * 2, 50)];
    NSArray *titleAry = @[@"手机号",@"储值金额",@"消费总额"];
    CGFloat width = (SCREEN_WIDTH - 15 * 2) / 3;
    for (int i = 0; i < titleAry.count; i ++) {
        CGFloat x = i * width;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 10, width, 30)];
        titleLabel.text = titleAry[i];
        titleLabel.font = FONT(16);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:titleLabel];
        
        if (i != 2) {
            CGFloat line_x = width * (i + 1);
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(line_x, 10, 1, 30)];
            [headerView addSubview:lineView];
            [CALayer drawDashLine:lineView lineLength:5 lineSpacing:5 lineColor:[UIColor lightGrayColor]];
        }
    }
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellMemberListInfo *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.isSelected = !cell.isSelected;
    ByModelAimMember *model = self.items[indexPath.row];
    if (cell.isSelected) {
        [self.selectedAry addObject:model];
    }else{
        [self.selectedAry removeObject:model];
    }
    if (self.selectedAry.count == self.cellArray.count) {
        _selectButton.selected = YES;
    }else{
        _selectButton.selected = NO;
    }
    if (self.memberListBlock) {
        self.memberListBlock([self.selectedAry mutableCopy]);
    }
    
}

#pragma mark - 全选
- (void)selectAction:(UIButton *) button{
    button.selected = !button.selected;
    if (button.selected) {
        for (ByCellMemberListInfo *cell in self.cellArray) {
            cell.isSelected = YES;
            self.selectedAry = [self.items mutableCopy];
        }
    }else{
        for (ByCellMemberListInfo *cell in self.cellArray) {
            cell.isSelected = NO;
            [self.selectedAry removeAllObjects];
        }
    }
    if (self.memberListBlock) {
        self.memberListBlock([self.selectedAry mutableCopy]);
    }
}

@end
